xlv.obj.desktop={
	extend:function(){
	},
	implement:function(){
		this.implement('xlv.imp.dmc');
	},
	pub:{
		showPane:{options:{modal:true,opacity:0.03,color:'white',cursor:'default',notifiedModalEvent:null,disposeEvent:null,left:null,top:null,invoker:null,align:'left',paneIn:null}}
	},
	properties:{
	},
	method:{
		construct:function(){
			var v=this.$V;
			Element.addClassName(this,'class_desktop');
			v.dock=xlv.util.findDirectChild(this,{xlvobject:'xlv.obj.dock'});
			if(v.dock)this.removeChild(v.dock);
			v.layout=xlv.util.findDirectChild(this,{xlvobject:'xlv.obj.borderLayout'},false);
			if(!v.layout){
				var temp=document.createElement('div');
				while(this.firstChild)temp.appendChild(this.firstChild);
				v.layout=xlv.sys.newObject('borderLayout');
				while(temp.firstChild)v.layout.appendChild(temp.firstChild);
				this.appendChild(v.layout);
			}
			this.appendChild(v.dock=(!v.dock)?xlv.sys.newObject('dock'):v.dock);
		},
		initial:function(){
			Event.observe(window,'resize',this.$V.resize=this._size.bindAsEventListener(this));
			//for test
			this.addEventHandler('xlv:select_changed','_xlv_select_changed');
			this.addEventHandler('xlv:active_changed','_xlv_active_changed');
		},
		styleFor:function(desktop){
		},
		_size:function(event){
			var frames=xlv.util.children(this,{xlvobject:'xlv.obj.frame','class':/(^|\s)maximized(\s|$)/},false);
			var loop=(Prototype.Browser.IE)?1:2;
			while(loop-->0){
				var dim=this.getDimensions();
				for(var i=0,n=frames.length;i<n;++i){
					var w=parseFloat(frames[i].style.width);
					var h=parseFloat(frames[i].style.height);
					var fdim=frames[i].getDimensions();
					var w2=(w+dim.width-fdim.width);
					var h2=(h+dim.height-fdim.height)-8;
					frames[i].setStyle({width:w2+'px',height:h2+'px'});
					frames[i].layout();
				}
			}
		},
		message:function(memo){
			//alert('desktop message :'+memo.type);
		},
		_xlv_select_changed:function(event){
			//alert('desktop : select_changed :'+xlv.util.inspect(event.target));
		},
		_xlv_active_changed:function(event){
			//alert('desktop : active_changed :'+xlv.util.inspect(event.target));
		},
		dispose:function(){
			Event.stopObserving(window,'resize',this.$V.resize);
		},
		systemModal:function(enable,opacity,color,cursor,notifiedModalEvent){
			var v=this.$V;
			if((enable &&v.glass)||(enable===false && !v.glass)) return;
			if(enable){
				v.glass=document.createElement('div');
				this.addEventHandler('mousedown','__systemModalEvent',v.glass);
				this.addEventHandler('click','__systemModalEvent',v.glass);
				this.addEventHandler('keydown','__systemModalEvent',v.glass);
				opacity=(!opacity)?0.3:opacity;
				color=(!color)?'white':color;
				cursor=(!cursor)?'wait':cursor;
				Element.setStyle(v.glass, {position:'fixed',left:'0px',top:'0px',right:'0px',bottom:'0px','opacity':opacity,background:color,cursor:cursor,zIndex:xlv.sys.zIndexSystemModal});
				this.appendChild(v.glass);
				v.notifiedModalEvent=notifiedModalEvent;
			}else{
				this.removeObserve('mousedown','__systemModalEvent',v.glass);
				this.removeObserve('click','__systemModalEvent',v.glass);
				this.removeObserve('keydown','__systemModalEvent',v.glass);
				v.glass.parentNode.removeChild(v.glass);
				v.glass=null;
				v.notifiedModalEvent=null;
			}
		},
		__systemModalEvent:function(event){
			event.stop();
			if(this.$V.notifiedModalEvent) this.$V.notifiedModalEvent(event);
		},
		showFrame:function(url,properties,method){
			var ajax=xlv.sys.newObject('ajaxFrame',{autoRequest:false});
			ajax.loaded=function(){
				ajax.$self('loaded');
				ajax.parentNode.removeChild(ajax);
			};
			this.appendChild(ajax);
			xlv.sys.initialize(ajax);
			ajax.request(url,properties,method);
		},
		showPane:function(url,properties,method,options){
			options=Object.extend(Object.extend({},xlv.obj.desktop.pub.showPane.options),(options||{}));
			var ajax=xlv.sys.newObject('ajaxPane',{autoRequest:false});
			Element.setStyle(ajax,{visibility:'hidden',left:'0px',top:'0px',position:'absolute',zIndex:xlv.sys.zIndexSystemModal});
			ajax._paneBounds=function(pane){
				var left=options.left,top=options.top;
				if(left==null||top==null){
					var dsk=Element.getDimensions(xlv.desktop);
					var dim=Element.getDimensions(pane);
					left=(dsk.width-dim.width)/2;
					top=(dsk.height-dim.height)/2;
					if(options.invoker){
						var offxy=xlv.desktop.viewportOffset(options.invoker);
						var pdim=Element.getDimensions(options.invoker);
						left=offxy.left;
						top=offxy.top+pdim.height;
						switch(options.align){
							case 'center':
							case 'middle':
								left+=(pdim.width-dim.width)/2;
								break;
							case 'right':
								left+=pdim.width-dim.width;
								break;
							case 'left':
						}
					}else{
						var lt=this.getDesktop().viewportOffset();
						left-= lt.left;
						top -= lt.top;
					}
				}
				ajax.style.left=((left<0)?0:left)+'px';
				ajax.style.top=((top<0)?0:top)+'px';
				ajax.style.visibility='visible';
				//
				pane.style.zIndex=xlv.sys.zIndexSystemModal;
				var glass=document.createElement('div');
				ajax.addEventHandler('mousedown','__systemModalEvent',glass);
				ajax.addEventHandler('click','__systemModalEvent',glass);
				ajax.addEventHandler('keydown','__systemModalEvent',glass);
				Element.setStyle(glass, {position:'fixed',left:'0px',top:'0px',right:'0px',bottom:'0px','opacity':options.opacity,background:options.color,cursor:options.cursor,zIndex:xlv.sys.zIndexSystemModal});
				ajax.insertBefore(glass,ajax.firstChild);
			};
			if(options.paneIn)ajax.$P.paneIn=options.paneIn;
			else if(options.align&&options.invoker)ajax.$P.paneIn=options.align;
			if(!options.disposeEvent){
				options.disposeEvent= function(){
					xlv.sys.dispose(ajax,true);
				}.bind(ajax);
			}
			if(!options.notifiedModalEvent){
				options.notifiedModalEvent=options.disposeEvent;
			}
			ajax.__systemModalEvent=function(){
				options.notifiedModalEvent();
			};
			ajax.xlv_dispose=function(event){
				if(event.memo.src==ajax.$V.pane){
					options.disposeEvent();
				}
			};
			this.appendChild(ajax);
			xlv.sys.initialize(ajax);
			ajax.request(url,properties,method);
			return ajax;
		},
		showNorthPane:function(onoff){
			this.$V.layout.showNorthPane(onoff);
		},
		showWestPane:function(onoff){
			this.$V.layout.showWestPane(onoff);
		},
		showCenterPane:function(onoff){
			this.$V.layout.showCenterPane(onoff);
		},
		showEastPane:function(onoff){
			this.$V.layout.showEastPane(onoff);
		},
		showSouthPane:function(onoff){
			this.$V.layout.showSouthPane(onoff);
		},
		setNorthPane:function(url,properties,method,pane_in){
			this.$V.layout.setNorthPane(url,properties,method,pane_in);
		},
		setWestPane:function(url,properties,method,pane_in){
			this.$V.layout.setWestPane(url,properties,method,pane_in);
		},
		setCenterPane:function(url,properties,method,pane_in){
			this.$V.layout.setCenterPane(url,properties,method,pane_in);
		},
		setEastPane:function(url,properties,method,pane_in){
			this.$V.layout.setEastPane(url,properties,method,pane_in);
		},
		setSouthPane:function(url,properties,method,pane_in){
			this.$V.layout.setSouthPane(url,properties,method,pane_in);
		},
		getPaneURL:function(pos){
			return this.$V.layout.getPaneURL(pos);
		},
		viewportOffset:function(element){
			if(element==null) return Element.viewportOffset(this);
			var lt=Element.viewportOffset(element);
			//IE paddingleft bug 
			if(Prototype.Browser.IE){
				var e=element;
				do {
					var left;
					if(left=Element.getStyle(e,'paddingLeft'))lt[0]-=parseInt(left);
					if(e.offsetParent==document.body&& Element.getStyle(e,'position')=='absolute') break;
				} while(e=e.offsetParent);
			}
			var desktop=Element.viewportOffset(this);
			lt[0]-=desktop[0];
			lt[1]-=desktop[1];
			lt.left=lt[0];
			lt.top=lt[1];
			return lt;
		},
		getOrderFrames:function(){
			var frames=xlv.util.children(this,{xlvobject:'xlv.obj.frame'},false);
			frames.sort(function(f1,f2){
				return (parseInt(f1.style.zIndex)||0)-(parseInt(f2.style.zIndex)||0);
			});
			return frames;
		},
		getDock:function(){
			return this.$V.dock
		},
		getActivatedFrame:function(){
			return xlv.util.children(this,{xlvobject:'xlv.obj.frame','class':/(^|\s)activated(\s|$)/},false);
		},
		processExclusiveFrame:function(url){
			var frame=this.getFrame(url);
			if(frame && frame.isExclusive()){
				this.activeFrame(frame);
				return true;
			}
			return false;
		},
		getFrame:function(url){
			var frames=xlv.util.children(this,{xlvobject:'xlv.obj.frame'},false);
			for(var i=0,n=frames.length;i<n;++i){
				if(frames[i].getUrl()==url)return frames[i];
			}
			return null;
		},
		addFrame:function(frame){
			var frames=this.getOrderFrames();
			var last=(frames.length>0)?frames[frames.length-1]:null;
			if(frame.isModal()||(last&&last.isModal())){
				var div=document.createElement('div');
				div.setAttribute('xlvobject','xlv.obj.frame');
				Element.addClassName(div,'modalglass');
				Element.setStyle(div,{position:'fixed',left:'0px',top:'0px',right:'0px',bottom:'0px'});
				this.appendChild(div);
				xlv.sys.shell(div);
				div.stopObserve();
				var max=(last)?(parseInt(last.getStyle('zIndex'))||0) :xlv.sys.zIndexModal;
				max=Math.max(max,xlv.sys.zIndexModal);
				Element.setStyle(div,{zIndex:max+1});
			}
			this.appendChild(frame);
		},
		disposeFrame:function(frame){
			if(frame.objectName()!='xlv.obj.frame')return false;
			var frames=this.getOrderFrames();
			var last=frames[frames.length-1];
			var last2=frames[frames.length-2];
			if(frame != last) return false;
			if(last2 && last2.hasClassName('modalglass')){
				xlv.sys.dispose(last2,true);
			}
			var shrink={element:frame};
			var w=parseInt(frame.style.width);
			var h=parseInt(frame.style.height);
			var x=parseInt(frame.style.left);
			var y=parseInt(frame.style.top);
			var save_overflow=frame.style.overflow;
			frame.style.overflow='hidden';
			shrink.style={left:x+w/2,top:y+h/2,width:24,height:24};
			shrink.processed=function(){frame.layout();};
			if(Prototype.Browser.IE){
			}else{
			}
			shrink.finished=function(){
				frame.style.overflow=save_overflow;
				frame.getDesktop().yieldActivated(frame);
				xlv.sys.dispose(frame,true);
			};
			xlv.util.styleEffect(shrink);
			return true;
		},
		activeFrame:function(frame,zindex_start){
			if(frame.objectName()!='xlv.obj.frame')return false;
			if(frame.hasClassName('iconified')){this.restoreIcon(frame);return true;}
			if(frame.hasClassName('activated'))return true;
			if(frame.hasClassName('modalglass'))return true;
			var max_zindex=(zindex_start||xlv.sys.zIndexFrame);
			//
			var frames=this.getOrderFrames();
			var last=frames[frames.length-1];
			if(last) max_zindex=Math.max((parseInt(last.getStyle('zIndex'))||0),max_zindex);
			for(var i=0,n=frames.length;i<n;++i) {
				frames[i].removeClassName('activated');
			}
			frame.setStyle({zIndex:max_zindex+1});
			Element.addClassName(frame,'activated');
			return true;
		},
		_sortWithzIndex:function(frames){
			frames.sort(function(f1,f2){
				return (parseInt(f1.style.zIndex)||0)-(parseInt(f2.style.zIndex)||0);
			});
		},
		yieldActivated:function(frame){
			var max_zindex=0;
			if(frame) frame.removeClassName('activated');
			var frames=xlv.util.children(this,{xlvobject:'xlv.obj.frame'},false);
			this._sortWithzIndex(frames);
			for(var i=frames.length-1;i>=0;--i){
				if(frames[i]==frame||frames[i].hasClassName('iconified'))continue;
				Element.addClassName(frames[i],'activated');
				return;
			}
		},
		iconifyFrame:function(frame){
			if(frame.objectName()!='xlv.obj.frame')return false;
			if(frame.hasClassName('iconified'))return true;
			if(frame.isModal()||parseInt(frame.style.zIndex)>=xlv.sys.zIndexModal)return false;
			if(frame.$V.effecting)return;
			frame.$V.effecting=true;
			var icon=(frame.$P.iconURL)?frame.$P.iconURL:xlv.obj.frame.pub.icon;
			frame.saveStyle();
			Element.addClassName(frame,'iconified');
			Element.removeClassName(frame,'activated');
			var dock=this.getDock();
			var icon_frame=dock.createIconFrameObject({iconURL:icon,_frameInstance:frame});
			icon_frame.style.display='none';
			var to_x=parseInt(icon_frame.style.left);
			var to_y=parseInt(icon_frame.style.top);
			icon_frame.style.left=frame.style.left;
			icon_frame.style.top=frame.style.top;
			this.appendChild(icon_frame);
			var shrink={element:frame};
			var move={element:icon_frame};
			shrink.style={width:'24px',height:'24px'};
			var save_overflow=frame.style.overflow;
			frame.style.overflow='hidden';
			shrink.processed=function(){frame.layout();};
			shrink.finished=function(){frame.style.display='none';icon_frame.style.display='';xlv.util.styleEffect(move);};
			move.style={left:to_x,top:to_y};
			move.finished=function(){
				frame.style.overflow=save_overflow;
				xlv.desktop.getDock().addIconFrameObject(icon_frame);
				xlv.desktop.yieldActivated();
				frame.$V.effecting=false;
			};
			xlv.util.styleEffect(shrink);
			return true;
		},
		restoreIcon:function(frame){
			var icon=this.getDock().removeIconFrameObject(frame);
			if(!icon)return;
			if(frame.$V.effecting)return;
			frame.$V.effecting=true;
			this.appendChild(icon);
			frame.removeClassName('iconified');
			var left=parseInt(frame.$V.saveStyle.left);
			var top=parseInt(frame.$V.saveStyle.top);
			var move={element:icon};
			var extend={element:frame};
			move.style={'left':left,'top':top};
			
			move.finished=function(){
				icon.parentNode.removeChild(icon);
				frame.removeClassName('iconified');
				frame.$V.saveStyle.display='none';
				frame.getDesktop().activeFrame(frame);
				frame.setStyle(frame.$V.saveStyle);
				frame.style.width='48px';
				frame.style.height='48px';
				frame.style.display='block';
				xlv.util.styleEffect(extend);
			};
			extend.style={width:frame.$V.saveStyle.width+'px',height:frame.$V.saveStyle.height+'px'};
			extend.processed=function(){frame.layout();};
			extend.finished=function(){frame.$V.effecting=false;};
				
			xlv.util.styleEffect(move);
		},
		maximumFrame:function(frame){
			if(frame.$V.effecting)return;
			frame.$V.effecting=true;
			frame.saveStyle();
			var dim=this.getDimensions();
			var w=parseFloat(frame.style.width);
			var h=parseFloat(frame.style.height);
			var w2=(w+dim.width-frame.offsetWidth);
			var h2=(h+dim.height-frame.offsetHeight)-24;
			var move={element:frame};
			var extend={element:frame,style:{width:w2,height:h2}};
			move.style={left:'0px',top:'0px'};
			if(Prototype.Browser.IE) {
				move.steps=-1;
			}
			move.finished=function(){
				Element.addClassName(frame,'maximized');
				frame.style.position="fixed";
				xlv.util.styleEffect(extend);
			};
			extend.processed=function(){frame.layout();};
			extend.finished=function(){frame.$V.effecting=false;}
			xlv.util.styleEffect(move);
		},
		restoreFrame:function(frame){
			if(frame.$V.effecting)return;
			frame.$V.effecting=true;
			frame.style.position="absolute";
			frame.removeClassName('maximized');
			var s=frame.$V.saveStyle;
			var shrink={element:frame};
			var move={element:frame};
			shrink.style={width:s.width,height:s.height};
			shrink.processed=function(){frame.layout();};
			shrink.finished=function(){xlv.util.styleEffect(move);};
			move.style={left:s.left,top:s.top};
			move.finished=function(){frame.$V.effecting=false;};
			xlv.util.styleEffect(shrink);
		}
	}
};
